/**
 * This file is Copyright © 2008 Software Craftsmen Limited. All Rights Reserved.
 */
package com.softwarecraftsmen.cachecontrol.keys;

import org.jetbrains.annotations.NotNull;

public abstract class AbstractKey<V> implements Key<V>
{
	public final int hashCode()
	{
		return hash();
	}

	public final boolean equals(final Object o)
	{
		return o != null && this.getClass().equals(o.getClass()) && exactlyMatches((Key<V>) o);
	}

	@SuppressWarnings("unchecked")
	public static @NotNull <V> Key<V> createKey(@NotNull final Object... orderedKeyComponents)
	{
		if (orderedKeyComponents.length == 0)
		{
			throw new IllegalArgumentException("A key must consist of at least one component");
		}
		else if (orderedKeyComponents.length == 1)
		{
			final Object keyComponent = orderedKeyComponents[0];
			return keyComponent instanceof Key ? (Key<V>) keyComponent : new SimpleKey<V>(keyComponent);
		}
		else
		{
			return new CompositeKey<V>(orderedKeyComponents);
		}
	}
}
